package com.young.cms.service.impl;

import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.interfaces.log.annotation.Log;
import com.young.cms.model.TemplateItem;
import com.young.cms.service.ICmsTemplateItemService;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* 代码模板服务实现
* Created by wangjiyu on 2022-2-2.
*/
@Service("templateItemService")
public class CmsTemplateItemServiceImpl implements ICmsTemplateItemService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(CmsTemplateItemServiceImpl.class);

    private static final String MAPPER = "com.young.cms.mapper.templateItem";//mapper的namespace

    //获取数据列表
    @Log("查询代码模板列表")
    @Override
    public List<TemplateItem> getList(TemplateItem obj) {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Log("分页查询代码模板列表")
    @Override
    public List<TemplateItem> getPage(TemplateItem obj, Page page) {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Log("查询代码模板")
    @Override
    public TemplateItem get(Long id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    //统计数量
    @Log("统计代码模板数量")
    @Override
    public int count(TemplateItem obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Log("修改代码模板")
    @Override
    public int update(TemplateItem obj) {
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Log("批量删除代码模板")
    @Override
    public int delete(Long[] idArr) {
        int i = 0;
        for(Long id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Log("删除代码模板")
    @Override
    public int delete(Long id) {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Log("新增代码模板")
    @Override
    public int insert(TemplateItem obj) {
        if (obj.getDataSequence() == null) {
            obj.setDataSequence(100L);
        }
        obj.setCreateTime(new Date());
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Log("批量新增代码模板")
    @Override
    public int batchInsert(List<TemplateItem> list) {
        int i = 0;
        for(TemplateItem item : list){
            i += this.insert(item);
        }
        return i;
    }

}